import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/widgets/appbar/common_app_bar.dart';
import 'package:flutter_yn_goods/widgets/image/auto_image.dart';
import 'package:flutter_yn_goods/widgets/widget_language_dialog.dart';
import 'package:flutter_yn_goods/widgets/widget_line.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'settings_logic.dart';

class SettingsPage extends StatelessWidget {
  final logic = Get.put(SettingsLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(
        title: LocaleKeys.settings.tr,
      ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.w),
        color: Colors.white,
        child: Column(
          children: [
            _buildSection(LocaleKeys.common_settings.tr, [
              _buildItem(Assets.mineRowLangue, LocaleKeys.change_language.tr,
                  onClick: () {
                const WidgetLanguageDialog().show();
              }),
              _buildItem(Assets.mineRowAbout, LocaleKeys.about_us.tr,
                  onClick: () {
                Get.toNamed(AppRoutes.aboutUsPage);
              }),
              _buildItem(Assets.mineRowService, LocaleKeys.contact_support.tr,
                  onClick: () {
                logic.openEmailApp();
              }),
            ]),
            SizedBox(height: 20.w),
            _buildSection(LocaleKeys.account_settings.tr, [
              _buildItem(Assets.mineRowCancel, LocaleKeys.cancel_account.tr,
                  textColor: Colors.red, onClick: () {
                if (AppUserManager().isLogin) {
                  logic.cancelAccount();
                } else {
                  Get.toNamed(AppRoutes.login);
                }
              }),
              _buildItem(Assets.exit, LocaleKeys.logout.tr, onClick: () {
                if (AppUserManager().isLogin) {
                  logic.loginOut();
                } else {
                  Get.toNamed(AppRoutes.login);
                }
              }),
            ]),
            SizedBox(height: 20.w),
            _buildSection(LocaleKeys.about_app.tr, [
              _buildItem(
                  Assets.mineRowVersion,
                  info: AppConfig.version,
                  LocaleKeys.current_version.tr,
                  onClick: () {}),
            ]),
          ],
        ),
      ),
    );
  }

  Widget _buildSection(String title, List<Widget> children) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.only(left: 12.w, bottom: 10.w),
          child: Text(
            title,
            style: TextStyle(
              color: AppColor.color616166,
              fontSize: 14.sp,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12.w),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.05),
                blurRadius: 6.w,
                offset: Offset(0, 2.w),
              ),
            ],
          ),
          child: Column(children: children),
        ),
      ],
    );
  }

  Widget _buildItem(String icon, String title,
      {required Function onClick, String? info, Color? textColor}) {
    return GestureDetector(
      onTap: () {
        onClick.call();
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: 54.w,
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12.w),
        ),
        child: Column(
          children: [
            Expanded(
                child: Row(
              children: [
                AutoImage(
                  icon,
                  width: 22.w,
                ),
                10.horizontalSpace,
                Expanded(
                    child: Text(
                  title,
                  style: TextStyle(
                      color: textColor ?? AppColor.color191D20,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w500),
                )),
                10.horizontalSpace,
                info != null
                    ? Text(
                        info,
                        style: TextStyle(
                            color: AppColor.color616166,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400),
                      )
                    : Icon(
                        Icons.arrow_forward_ios,
                        size: 14.w,
                        color: AppColor.color616166,
                      )
              ],
            )),
            Padding(
              padding: EdgeInsets.only(left: 32.w),
              child: const WidgetLine(),
            ),
          ],
        ),
      ),
    );
  }
} 